package com.itjiange.servlet;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.itjiange.utils.RandomCodeUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 发送邮件
 *
 * @author ItJiange
 * @date 2023/10/19
 */
@WebServlet("/sendEmail")
public class SendEmailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MailAccount account = new MailAccount();
        account.setPort(25);
        account.setAuth(true);
        account.setFrom("1403071979@qq.com");
        account.setUser("1403071979");
        account.setPass("rohdsbmfjuzbihac");

        // 验证码
        String randomStr = RandomCodeUtils.randomString();
        Cookie cookie = new Cookie("RANDOM_CODE", randomStr);
        resp.addCookie(cookie);
        System.out.println(randomStr);
        MailUtil.send(account, CollUtil.newArrayList("m13919991962@163.com"), "验证码", randomStr, false);
    }
}
